//
//  OO_AddAddressViewController.m
//  ShopProject
//
//  Created by macbookpro on 2018/9/28.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "OO_AddAddressViewController.h"
#import "FSTextView.h"
#import "YJLocationPicker.h"
@interface OO_AddAddressViewController ()
@property (weak, nonatomic) IBOutlet UITextField *peopleTf;
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UIButton *cityButton;
@property (weak, nonatomic) IBOutlet FSTextView *detailAdressTf;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@end

@implementation OO_AddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self UIConfig];
}

-(void)UIConfig {
    self.title = @"添加收货地址";
    _saveBtn.backgroundColor = MainColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (IBAction)chooseAddressButtonClicked:(UIButton *)sender {
    [self.view endEditing:YES];
    //直接调用
    [[[YJLocationPicker alloc] initWithSlectedLocation:^(NSArray *locationArray) {
        
        //array里面放的是省市区三级
        NSLog(@"%@", locationArray);
        //拼接后给button赋值
        [sender setTitle:[locationArray componentsJoinedByString:@""] forState:UIControlStateNormal];
        
    }] show];
    
}

- (IBAction)saveButtonPress:(UIButton *)sender {
    
    if (!self.peopleTf.text.length) {
        [MBProgressHUD showErrorMessage:@"请输入收货人姓名"];
        return;
    }
    
    if (!self.phoneTf.text.length) {
        [MBProgressHUD showErrorMessage:@"请输入手机号码"];
        return;
    }
    else{
        if (![CommonAPI telPhoneCheck:self.phoneTf.text]) {
            [MBProgressHUD showErrorMessage:@"手机号码不正确"];
            return;
        }
        
    }
    if ([self.cityButton.titleLabel.text isEqualToString:@"选择地区"]) {
        [MBProgressHUD showErrorMessage:@"请选择您所在的区域"];
        return;
    }
    
    if (![self.detailAdressTf.text length]) {
        [MBProgressHUD showErrorMessage:@"请输入您的详细地址"];
        return;
    }
    
    
    JQFMDB *fmdb = [JQFMDB shareDatabase];
    OO_AddressModel *model = [OO_AddressModel new];
    
    model.peopleName = self.peopleTf.text;
    model.phone = self.phoneTf.text;
    model.city = self.cityButton.titleLabel.text;
    model.adress = [NSString stringWithFormat:@"%@",self.detailAdressTf.text];
    model.ID = [CommonAPI getOnlyId];
    model.zhangHao = [UserDefaults readFromPlist:LOGIN_STATUS_TEL];
    WeakSelf
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if ( [fmdb jq_insertTable:ADDRESSDATABASE dicOrModel:model]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showTipMessageInWindow:@"添加成功"];
                if (weakSelf.addSuccess) {
                    weakSelf.addSuccess();
                }
                [weakSelf.navigationController popViewControllerAnimated:YES];
          
            });
        }
        else{
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD showTipMessageInWindow:@"添加失败,请重试"];

            
        });
        
        }
    });
    
    
    
    
}


@end
